package cn.e4j.modular.system.controller;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.modular.system.entity.SysTableColumn;
import cn.e4j.modular.system.service.impl.SysTableColumnServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * 表结构维护
 *
 * @author miaoyl
 * @Date 2018/12/08 16:03
 */
@Controller
@RequestMapping("/sys/table")
public class SysTableColumnsController extends BaseController {

    @Autowired
    private SysTableColumnServiceImpl sysTableColumnService;

    @Autowired
    private DynamicDataSourceProperties dynamicDataSourceProperties;

    /**
     * 跳转表结构管理页面
     */
    @RequiresPermissions("sys:table:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/table.html";
    }

    /**
     * 查询表结构信息列表
     */
    @RequestMapping("/getTablelist")
    @RequiresPermissions("sys:table:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysTableColumnService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加
     */
    @RequestMapping("/add")
    @RequiresPermissions("sys:table:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysTableColumn> tableList = inDto.getReqList(SysTableColumn.class);
        List<Map<String, Object>> tableDto = (List<Map<String, Object>>)(inDto.get("_jsonStr_"));
        Dto outDto = sysTableColumnService.addData(tableList, tableDto);
        return ResponseData.success(outDto);
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:param:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysTableColumn> tableList = inDto.getReqList(SysTableColumn.class);
        List<Map<String, Object>> tableDto = (List<Map<String, Object>>)(inDto.get("_jsonStr_"));
        Dto outDto = sysTableColumnService.editData(tableList, tableDto);
        return ResponseData.success(outDto);
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:table:del")
    @ResponseBody
    public ResponseData del(SysTableColumn sysTableColumn) {
        sysTableColumnService.delete(sysTableColumn);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("sys:table:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<SysTableColumn> sysTableColumnList = inDto.getReqList(SysTableColumn.class);
        sysTableColumnService.delDataBatch(sysTableColumnList);
        return SUCCESS_TIP;
    }

    /**
     * 根据表名查询表结构信息列表
     */
    @RequestMapping("/getTableEdit")
    @ResponseBody
    public ResponseData getTableEdit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = sysTableColumnService.getTableData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 根据表名获取表中数据
     */
    @RequestMapping("/getTableCount")
    @ResponseBody
    public ResponseData getTableCount(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = sysTableColumnService.getTableCount(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 测试数据库
     */
    @RequestMapping("/delTest")
    @ResponseBody
    public ResponseData delTest(@DtoParam DtoWapper dtoWapper){
        Properties pros = new Properties();
        Dto inDto = dtoWapper.getDto();
        //JSONObject jsonObject = inDto.getReqJsonObject();
        DataSourceProperty dataSource = dynamicDataSourceProperties.getDatasource().get("master");
        if(!(dataSource.getUsername().equals(inDto.getString("USERNAME")) && dataSource.getPassword().equals(inDto.getString("PASSWORD")))){
           /* List<SysTableColumn> sysTableColumnList = JSONObject.parseArray(jsonObject.getString("_xdata"), SysTableColumn.class);
            sysTableColumnService.delDataBatch(sysTableColumnList);*/
            return ResponseData.error("数据库账号密码错误！");
        }
        return ResponseData.success();
    }

    /**
     * 获取数据库类型
     */
    @RequestMapping("/getdbtype")
    @ResponseBody
    public ResponseData getdbtype(@DtoParam DtoWapper dtoWapper){
        Properties pros = new Properties();
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        DataSourceProperty dataSource = dynamicDataSourceProperties.getDatasource().get("master");
        if(dataSource.getDriverClassName().contains("oracle")){
           outDto.put("dbType", false);
        }else {
            outDto.put("dbType", true);
        }
        return ResponseData.success(outDto);
    }
}
